<?php
namespace Home\Controller;
use Think\Controller;
class HomeController extends Controller {
	public $wechat;
	//public $wechats;
	public $userinfo;
	public $signPackage;

	/**
	 * 初始化
	 */
	public function __construct() {
		parent::__construct();
		/*
		$mobile = isMobile();
		if(!$mobile) {
			header("Content-type:text/html;charset=utf-8");
			echo '<div style="width:100%;text-align:center;line-height:100px;margin-top:250px; font-size:30px;">欢迎访问http://zhumeng777.com/请用手机微信浏览相关内容</div>';
			echo '<div style="width:100%;text-align:center;margin-top:100px;">吉ICP备15003512号-2</div>';
			die();
		}
		*/

//		Vendor('Wechat.Wechat');
//		$this->wechat = new \Wechat('token',C('CORPID'),C('SECRET'));
//		$this->getUserinfo();
		/**
		 * 取出个人信息 
		 */
		$userinfo = D('address')->where(array('openid'=>$_SESSION['info']['openid']))->find();
		if(empty($userinfo)) { // 判断是否有这个人
			$data['openid'] = $_SESSION['info']['openid'];
			$data['nickname'] = $_SESSION['info']['nickname'];
			$data['sex'] = $_SESSION['info']['sex'];
			//$data['city'] = $_SESSION['info']['city'];
			//$data['province'] = $_SESSION['info']['province'];
			//$data['country'] = $_SESSION['info']['country'];
			$data['headimgurl'] = $_SESSION['info']['headimgurl'];
			$data['time'] = time();
			$data['update'] = time();
			$pings = Pinyin(trim(str_replace(array(1,2,3,4,5,6,7,8,9,0),'',$data['nickname'])));
			if(empty($pings)) {
				$data['ping'] = 'h';
			} else {
				$data['ping'] = $pings;
			}
			$msg = D('address')->add($data);
			//$data['id']	= $msg;
			$this->userinfo = D('address')->where(array('id'=>$msg))->find();
		} else { // 已经存在
			if($_SESSION['info']['headimgurl'] != $userinfo['headimgurl']) {
				$data['headimgurl'] = $_SESSION['info']['headimgurl'];
				$msg = D('address')->where(array('id'=>$userinfo['id']))->save($data);
			}		
			$this->userinfo = $userinfo;
		}
		$this->assign('uid',$this->userinfo['id']);
		//$this->assign('phone','0431-8086-1956');
		$this->assign('phone','0431-81668165');

		Vendor('Wechat.Wechats');
		$option = array(
			'corpid'=>C('CORPID'),
			'secret'=>C('SECRET'),
		);
		$wechats = new \Wechats($option);
		$access = $wechats->getAccesstoken();
		$jsdk = $wechats->getJsapiticket();
		$this->signPackage = $wechats->getSignPackage();                                                                                                                               
		
	}
	
	/**
	 * 获取用户信息
	 */
	protected function getUserinfo() {
		$session = I('session.info','','strip_tags');
		if(empty($session)) {
			$code = I('get.code','','strip_tags');
			if(empty($code)) {
				//$url = "http://".$_SERVER['HTTP_HOST'].U(MODULE_NAME.'/'.CONTROLLER_NAME.'/'.ACTION_NAME);
				$url = C("REDIRECT_DOMAIN").__SELF__;
				$location = $this->wechat->oauthGetCode($url,'snsapi_userinfo',1);
				header('Location:'.$location);
				die();
			}

			$data = $this->wechat->oauthGetAccessToken($code);
			if(!empty($data['errcode'])) {
				errorLog('oauthGetAccessToken error:'.$data['errcode']);
				die();
			}

			$info = $this->wechat->oauthUserInfo($data['access_token'],$data['openid']);
			if(!empty($info['errcode'])) {
				errorLog('oauthUserInfo error:'.$data['errcode']);
				die();
			}

			$_SESSION['info'] = $info;
		}
	}

}
